﻿//========================================
// All rights reserved (C) 2017-2100
// CLR 版本：	4.0.30319.42000
// 命名空间:	SFJDService
// 文 件 名：	GetAvailablePort
// 作    者：	WooG
// 联系方式：	wooglle@163.com
// 创建时间：	2018/6/22 22:43:30
//========================================
using System.Collections;
using System.Net;
using System.Net.NetworkInformation;

namespace SFJDService
{
    static class GetAvailablePort
    {
        /// <summary>
        /// 获取指定个数的可用端口
        /// </summary>
        /// <param name="mount"></param>
        /// <returns></returns>
        public static int[] GetAvailablePorts(int mount)
        {
            int MAX_PORT = 60000; //系统tcp/udp端口数最大是65535
            int BEGIN_PORT = 50000;//从这个端口开始检测
            int[] b = new int[mount];
            IList portUsed = PortIsUsed();

            for (int i = 0; i < mount; i++)
            {
                for(int j = BEGIN_PORT; j < MAX_PORT; j++)
                {
                    bool isAvailable = true;
                    foreach (int p in portUsed)
                    {
                        if (p == j)
                        {
                            isAvailable = false;
                            break;
                        }
                    }
                    if (isAvailable)
                    {
                        b[i] = j;
                        BEGIN_PORT = j + 1;
                        break;
                    }
                }
            }
            return b;
        }

        /// <summary>
        /// 获取第一个可用的端口号
        /// </summary>
        /// <returns></returns>
        public static int GetFirstAvailablePort()
        {
            int MAX_PORT = 6000; //系统tcp/udp端口数最大是65535            
            int BEGIN_PORT = 5000;//从这个端口开始检测

            for (int i = BEGIN_PORT; i < MAX_PORT; i++)
            {
                if (PortIsAvailable(i)) return i;
            }

            return -1;
        }

        /// <summary>
        /// 获取操作系统已用的端口号
        /// </summary>
        /// <returns></returns>
        public static IList PortIsUsed()
        {
            //获取本地计算机的网络连接和通信统计数据的信息
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

            //返回本地计算机上的所有Tcp监听程序
            IPEndPoint[] ipsTCP = ipGlobalProperties.GetActiveTcpListeners();

            //返回本地计算机上的所有UDP监听程序
            IPEndPoint[] ipsUDP = ipGlobalProperties.GetActiveUdpListeners();

            //返回本地计算机上的Internet协议版本4(IPV4 传输控制协议(TCP)连接的信息。
            TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();

            IList allPorts = new ArrayList();
            foreach (IPEndPoint ep in ipsTCP) allPorts.Add(ep.Port);
            foreach (IPEndPoint ep in ipsUDP) allPorts.Add(ep.Port);
            foreach (TcpConnectionInformation conn in tcpConnInfoArray) allPorts.Add(conn.LocalEndPoint.Port);

            return allPorts;
        }

        /// <summary>
        /// 检查指定端口是否已用
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public static bool PortIsAvailable(int port)
        {
            bool isAvailable = true;

            IList portUsed = PortIsUsed();

            foreach (int p in portUsed)
            {
                if (p == port)
                {
                    isAvailable = false; break;
                }
            }

            return isAvailable;
        }
    }
}
